package com.zretc.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * 反射工具包
 */
public class ReflectUtil {

    private static final Logger logger = LoggerFactory.getLogger(ReflectUtil.class);

    public static String getValue(String key, Object obj) {

        String value = "";

        try {

            // 获取当前属性
            PropertyDescriptor pd = new PropertyDescriptor(key, obj.getClass());
            // 获取get方法
            Method getMd = pd.getReadMethod();
            // 执行方法
            value = getMd.invoke(obj).toString();
        } catch (Exception e) {

            logger.error("获取内容失败！");
            e.printStackTrace();
        }

        return value;
    }

    public static void setValue(String key, String value, Object obj) {

        try {

            // 获取当前属性
            PropertyDescriptor pd = new PropertyDescriptor(key, obj.getClass());
            // 获取set方法
            Method writeMd = pd.getWriteMethod();
            writeMd.invoke(obj, value);
        } catch (Exception e) {

            logger.error("设置内容失败！");
            e.printStackTrace();
        }
    }


}
